package com.yanqu.road.server.cmd.activity.xiayue;

import com.yanqu.road.entity.activity.xiayue.config.XiaYueConfig;
import com.yanqu.road.pb.activity.XiaYueActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.xiayue.XiaYueModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.xiayue.XiaYueMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_XIAYUE_SEARCH, desc = "夏越活动搜寻")
public class XiaYueSearchCmd extends AbstractCommand {

    private int times;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        XiaYueActivityProto.XiaYueSearchReqMsg reqMsg = XiaYueActivityProto.XiaYueSearchReqMsg.parseFrom(packet.getBytes());
        times = reqMsg.getTimes();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        XiaYueConfig xiaYueConfig = XiaYueMgr.getXiaYueConfig();
        if (xiaYueConfig == null || !ActivityMgr.activityInTime(xiaYueConfig.getActivityInfo())) {
            ret = GameErrorCode.E_XIAYUE_NO_IN_TIME;
            return;
        }

        ret = player.getModule(XiaYueModule.class).search(times);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            XiaYueActivityProto.XiaYueSearchRespMsg.Builder builder = XiaYueActivityProto.XiaYueSearchRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_XIAYUE_SEARCH, builder);
        }
    }
}
